<template>
  <div>
    <el-tree :data="menus" :props="defaultProps"   @node-click="handleNodeClick" :expand-on-click-node="false"  node-key="catId" show-checkbox  :default-expanded-keys="isexpanded">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button v-if="node.level <= 2" type="text" size="mini" @click="() => append(data)">
            Append
          </el-button>
          <el-button  type="text" size="mini" @click="() => edit(data)">
            Edit
          </el-button>
          <el-button v-if="node.childNodes.length == 0"  type="text" size="mini" @click="() => remove(node, data)">
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :model="category">
        <el-form-item label="分类或商品" >
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="category">
        <el-form-item label="图标" >
          <el-input v-model="category.icon" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="category">
        <el-form-item label="计量单位" >
          <el-input v-model="category.productUnit" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitData()" >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        dialogtype: '',
        dialogVisible: false,
        menus: [],
        isexpanded: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        category: {
          name: '',
          parentCid: 0,
          catLevel: 0,
          showStatus: 1,
          sort: 0,
          catId: null,
          icon: '',
          productUnit: ''
        }
      }
    },
    methods: {
      handleNodeClick (data) {
        console.log(data)
      },
      getMenus () {
        this.$http({
          url: this.$http.adornUrl('/product/category/list'),
          method: 'get'
        }).then(({
          data
        }) => {
          this.menus = data.data
          console.log('成功获取到数据', data.data)
        })
      },
      append (data) {
        console.log('append', data)
        this.dialogtype = 'add'
        this.title = '增加分类'
        this.dialogVisible = true
        this.category.parentCid = data.catId
        this.category.catLevel = data.catLevel * 1 + 1
      },
      addcategory () {
        console.log('提交的三级分类数据', this.category)
        this.$http({
          url: this.$http.adornUrl('/product/category/save'),
          method: 'post',
          data: this.$http.adornData(this.category, false)
        }).then(({data}) => {
          this.$message({
            message: '保存成功',
            type: 'success'
          })
          this.category.name = ' '
          // 关闭对话框
          this.dialogVisible = false
          this.getMenus()
          this.isexpanded = [this.category.parentCid]
        })
      },
      editcategory () {

      },
      remove (node, data) {
        this.$confirm(`是否删除【${data.name}】菜单?`, '提示', {
          confirmButtonText: '确定删除',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var ids = [data.catId]
          this.$http({
            url: this.$http.adornUrl('/product/category/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getMenus()
            this.isexpanded = [node.parent.data.catId]
          })
        }).catch(() => {

        })
        console.log(node, data)
      },
      edit (data) {
        console.log(data)
        this.dialogtype = 'edit'
        this.title = '修改分类'
        this.dialogVisible = true
        this.category.name = data.name
        this.catId = data.catId
      },
      submitData () {
        if (this.dialogtype === 'add') {
          this.addcategory()
        }
        if (this.dialogtype === 'edit') {
          this.editcategory()
        }
      }
    },
    created () {
      this.getMenus()
    }
  }
</script>

<style>
</style>
